﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class administration_listClientCourse : System.Web.UI.Page
{
    private const string IDPAGE = "26";

    protected void Page_Load(object sender, EventArgs e)
    {
        //Check the status of the session
        STATE_SESSION();

        //Verify the view that corresponds show
        if (!Page.IsPostBack)
        {
            string flgInsert = Request.Cookies.Get("flgInsert").Value;
            string flgUpdate = Request.Cookies.Get("flgUpdate").Value;
            string flgDelete = Request.Cookies.Get("flgDelete").Value;

            //Disable the checks after loading the datalist
            UpdateInsertDelete(flgUpdate, flgInsert, flgDelete);
            //Built the menu according to user roles
            BuildMenu();
        }
    }

    //Method to verify the status of the current session
    public void STATE_SESSION()
    {
        if (!Page.IsPostBack)
        {
            HttpCookie state = Request.Cookies.Get("state");
            if (state == null)
                Response.Redirect("../index.aspx");
        }
    }

    //Method to verify the user's role
    private void UpdateInsertDelete(string update, string insert, string delete)
    {
        if (!insert.Equals("1"))
        {
            btnAdd.Enabled = false;
        }
        if (!delete.Equals("3"))
        {
            btnRemove.Enabled = false;
        }
    }

    //Method to build menu
    private void BuildMenu()
    {
        string programName = Request.Cookies.Get("programName").Value;
        char[] delimiterChars = { ',' };
        string[] vectorProgramName = programName.Split(delimiterChars);
        string welcome = "";
        string segurity = "";
        string system = "";
        string parameters = "";
        string people = "";
        string operations = "";
        string reports = "";
        string profile = "";
        for (int i = 0; i < vectorProgramName.Length; i++)
        {
            switch (Convert.ToInt32(vectorProgramName[i]))
            {
                case 1:
                    people += "<li><a class=\"item\" href=\"listNaturalPersons.aspx\"> - Personas Naturales</a></li>";
                    break;
                case 2:
                    people += "<li><a class=\"item\" href=\"listLegalPersons.aspx\"> - Personas Legales</a></li>";
                    break;
                case 3:
                    welcome += "<li><a class=\"item\" href=\"index.aspx\"> - Bienvenido</a></li>";
                    break;
                case 4:
                    parameters += "";
                    break;
                case 5:
                    system += "<li><a class=\"item\" href=\"listLogTransaction.aspx\"> - Log de Transacciones</a></li>";
                    break;
                case 6:
                    operations += "<li><a class=\"item\" href=\"listNews.aspx\"> - Boletines</a></li>";
                    break;
                case 7:
                    operations += "<li><a class=\"item\" href=\"listClient.aspx\"> - Clientes</a></li>";
                    break;
                case 8:
                    segurity += "<li><a class=\"item\" href=\"listProfiles.aspx\"> - Perfiles</a></li>";
                    break;
                case 9:
                    system += "<li><a class=\"item\" href=\"listPrograms.aspx\"> - Programas</a></li>";
                    break;
                case 10:
                    operations += "<li><a class=\"item\" href=\"listCourse.aspx\"> - Cursos</a></li>";
                    break;
                case 11:
                    segurity += "<li><a class=\"item\" href=\"listRoles.aspx\"> - Roles</a></li>";
                    break;
                case 12:
                    segurity += "<li><a class=\"item\" href=\"listUsers.aspx\"> - Usuarios</a></li>";
                    break;
                case 13:
                    reports += "<li><a class=\"item\" href=\"reports.aspx\"> - Clientes por tipo de Servicio</a></li>";
                    break;
                case 14:
                    system += "<li><a class=\"item\"href=\"listWorkArea.aspx\"> - Areas</a></li>";
                    break;
                case 15:
                    parameters += "<li><a class=\"item\"href=\"listMode.aspx\"> - Modalidades</a></li>";
                    break;
                case 16:
                    parameters += "<li><a class=\"item\"href=\"listProfession.aspx\"> - Profesiones</a></li>";
                    break;
                case 17:
                    operations += "<li><a class=\"item\"href=\"listPublicity.aspx\"> - Publicidades</a></li>";
                    break;
                case 18:
                    operations += "<li><a class=\"item\"href=\"listQuestionAnswer.aspx\"> - Consultas del Cliente</a></li>";
                    break;
                case 19:
                    parameters += "<li><a class=\"item\"href=\"listService.aspx\"> - Servicios</a></li>";
                    break;                
                case 21:
                    operations += "<li><a class=\"item\"href=\"listServiceProfileClient.aspx\"> - Perfiles de Clientes</a></li>";
                    break;
                case 22:
                    operations += "<li><a class=\"item\"href=\"listSponsor.aspx\"> - Auspiciadores</a></li>";
                    break;
                case 23:
                    parameters += "<li><a class=\"item\"href=\"listYear.aspx\"> - Gestiones</a></li>";
                    break;
                case 24:
                    profile += "<li><a class=\"item\"href=\"listProfileClientService.aspx\"> - Servicios</a></li>";
                    break;
                case 25:
                    profile += "<li><a class=\"item\"href=\"listProfileClientCourse.aspx\"> - Cursos</a></li>";
                    break;
                case 26:
                    operations += "<li><a class=\"item\"href=\"listClientCourse.aspx\"> - Cursos del Cliente</a></li>";
                    break;
                case 27:
                    profile += "<li><a class=\"item\"href=\"listProfileQA.aspx\"> - Consultas</a></li>";
                    break;
            }
        }
        lblMenu.Text = "<div class=\"left\"><ul class=\"title\"><li>Menu Administrativo</li></ul><ul id=\"menu\"><li>";
        lblMenu.Text += "<a href=\"#\">Bienvenido</a><ul>";
        lblMenu.Text += welcome;
        lblMenu.Text += "</ul></li>";
        if (!segurity.Equals("")) {
            lblMenu.Text += "<li><a href=\"#\">Gestion de Seguridad</a><ul>";
            lblMenu.Text += segurity;
            lblMenu.Text += "</ul></li>";
        }
        if (!system.Equals("")) {
            lblMenu.Text += "<li><a href=\"#\">Gestion del Sistema</a><ul>";
            lblMenu.Text += system;
            lblMenu.Text += "</ul></li>";
        }
        if (!parameters.Equals("")) {
            lblMenu.Text += "<li><a href=\"#\">Gestion de Parametros</a><ul>";
            lblMenu.Text += parameters;
            lblMenu.Text += "</ul></li>";
        }
        if (!people.Equals("")) {
            lblMenu.Text += "<li><a href=\"#\">Gestion de Personas</a><ul>";
            lblMenu.Text += people;
            lblMenu.Text += "</ul></li>";
        }
        if (!operations.Equals("")) {
            lblMenu.Text += "<li><a href=\"#\">Gestion de Operaciones</a><ul>";
            lblMenu.Text += operations;
            lblMenu.Text += "</ul></li>";
        }
        if (!profile.Equals("")) {
            lblMenu.Text += "<li><a href=\"#\">Perfil del Cliente</a><ul>";
            lblMenu.Text += profile;
            lblMenu.Text += "</ul></li>";
        }
        if (!reports.Equals("")) {
            lblMenu.Text += "<li><a href=\"#\">Reportes</a><ul>";
            lblMenu.Text += reports;
            lblMenu.Text += "</ul></li>";
        }
        lblMenu.Text += "<li><a href=\"../index.aspx\">Salir</a></li>";
        lblMenu.Text += "</ul></div>";
    }

    protected void cbClient_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DataSourceLoad();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!listboxClientCourse.Items.Contains(listboxCourse.SelectedItem) && (listboxCourse.SelectedItem != null))
        {
            clsClientCourse obj = new clsClientCourse();
            obj.IdCourse = Convert.ToInt32(listboxCourse.SelectedValue);
            obj.IdClient = Convert.ToInt32(cbClient.SelectedValue);
            obj.Insert();
            DataSourceLoad();
            lblMessage.Text = " ";
        }
        else
        {
            lblMessage.Text = "Debe seleccionar algun curso o el curso ya ha sido agregado.";
        }
    }

    private void DataSourceLoad()
    {
        listboxClientCourse.DataSourceID = "ODSConsultClientCourseID";
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if ((listboxClientCourse.SelectedItem != null))
        {
            clsClientCourse obj = new clsClientCourse();
            obj.ID = Convert.ToInt32(listboxClientCourse.SelectedValue);
            obj.Delete();
            DataSourceLoad();
            lblMessage.Text = " ";
        }
        else
        {
            lblMessage.Text = "Debe seleccionar algun curso.";
        }
    }
}